package com.lxg.service.impl;

import com.lxg.dao.AccountDao;
import com.lxg.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * @author 陆小根
 * date: 2022/04/27
 * Description:
 */

@Service
public class AccountServiceImpl implements AccountService {

  private final AccountDao accountDao;

  @Autowired
  public AccountServiceImpl(AccountDao accountDao) {
    this.accountDao = accountDao;
  }

  @Override
  @Transactional(rollbackFor = {IOException.class})
  public void transfer(String out, String in, Double money) throws IOException {
    accountDao.outMoney(out, money);
    // int i = 1 / 0; // 这个异常事务会回滚
//    if (true) throw new IOException(); // 这个异常事务就不会回滚了
    accountDao.inMoney(in, money);
  }
}
